package cn.tedu.v6;

import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 *    Version v4  客户端    登录   退出: 退两次
 *
 */

public class Client {
    private static final String HOST="192.168.194.117";
    private static final Integer PORT=7777;
    private Socket socket;
    private String ip;
    private PrintWriter pw;
    private BufferedReader br;



    public Client() {
        try {
            socket = new Socket(HOST,PORT);
            ip = socket.getInetAddress().getHostAddress();
            br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
            pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8")), true);


        } catch (SocketException e) {
            System.out.println("客户端链接被重置");
        } catch (UnknownHostException e) {
            System.out.println("IP地址有问题 请询问服务器端!");
        } catch (IOException e) {
            System.out.println("客户端主动退出了: 断开连接");
        }
    }
    public void start() {
        System.out.println("欢迎使用**牌聊天室");
        Scanner input=new Scanner(System.in);
        //目的: 不捕获 确定没有问题 捕获代码块具体结构
        try {
            //①. 接收登录提示
            //1. 读入服务器端--输出流
            String serverMessage=br.readLine(); //服务器端: 写"请输入用户名: "
            System.out.println(serverMessage);   //读入账号,必须写出
            //2. 写出用户名
            String username=input.nextLine();  //客户端开始输入用户名
            //3. 写出用户名,发给服务器
            pw.println(username);  //写出给服务器端---获取账号

            //② 接收登录密码提示
            //1. 读入客户端端--输出流
            serverMessage=br.readLine();  //服务器写: "请输入密码"
            System.out.println(serverMessage);   //打印"请输入密码"
            //2. 写出密码
            String password=input.nextLine();
            //3. 写出密码,发给服务器
            pw.println(password);

            //③ 服务器端验证账号密码是否正确
            //3.1 读入 账号和密码正确的情况
            serverMessage=br.readLine();  //登录成功 欢迎进入XXX牌聊天室
            System.out.println(serverMessage);  //客户端打印: "登陆成功"
            if(serverMessage.startsWith("登陆成功")){  //true---聊天
                chat();  //聊天功能
            }

        } catch (IOException e){
            System.out.println("客户端断开连接");
        }
            finally {
            if (pw != null)
                pw.close();
            if (br != null)
                try {
                    br.close();
                } catch (IOException e) {
                    System.out.println("服务器通讯内容失败");
                }
            if (socket != null && !socket.isClosed()) {
                System.out.println();
                try {
                    socket.close();
                } catch (IOException e) {
                    System.out.println("服务器退出连接异常");
                }
            }
            System.out.println(ip + "客户端退出了服务器连接");
        }
    }

   //1. start方法外----封装  客户端 相互通话方法-chat\
    private void chat(){
        new Thread(() -> {
            try {
                String message;
                while ((message = br.readLine()) != null) {
                    System.out.println(message);
                    System.out.println("客户端请输入: ");

                }
            } catch (IOException e) {
                System.out.println("服务器发来的消息断开了");
            }
        }).start();
        Scanner input = new Scanner(System.in);
        while (true) {
            System.out.println("客户端请输入: ");
            String say = input.nextLine();
            pw.println("说: " + say);

            if(say.equals("exit")){
                System.exit(0);
            }

        }

    }
    public static void main(String[] args) {
        new Client().start();
    }
}
